<?php

// fetch_node_xml.php
// Michael Baker
// 2010.08.22
//
// Fetch XML data describing nodes in a certain MA's database

define("IN_MDM", true);
include '../includes/all.php';
header("Content-Type: text/plain");

$ip = $_GET[ "ip" ];
$xml = new DOMDocument('1.0');
$xml->formatOutput = true;

$root = $xml->createElement( 'root' );
$root = $xml->appendChild( $root );

// Initialize an element containing information about the node itself
$info = $xml->createElement( 'info' );
$info = $root->appendChild( $info );

$infoStatus = $xml->createElement( 'responds' );
$infoStatus = $info->appendChild( $infoStatus );

// We can't assume that the node is in its own NODES table, so we'll leave this out for now
// $infoTitle  = $xml->createElement('title');
// $infoTitle  = $info->appendChild($infoTitle);

$nodes = $xml->createElement( 'nodes' );
$nodes = $root->appendChild( $nodes );
 
$responds = device_responds( $ip );

if ( $responds )
{
  $status = $xml->createTextNode( '1' );
  $status = $infoStatus->appendChild( $status );

	$link = new QueryDatabase( $ip );
	$link->set_date( get_ymd( time() ) );	
	$infos = $link->get_nodes();
	
	foreach ( $infos as $arr )
	{
		$n_id = $arr[ 0 ];
		$n_name = $arr[ 1 ];
		$n_title = $arr[ 2 ];
		$n_addr = $arr[ 3 ];

		$node = $xml->createElement( 'node' );
		$node = $nodes->appendChild( $node );

		$owampID = $xml->createElement( 'owamp', $n_id );
		$owampID = $node->appendChild( $owampID );

		$shortName = $xml->createElement( 'sname', $n_name );
		$shortName = $node->appendChild( $shortName );

		$longName = $xml->createElement( 'lname', $n_title );
		$longName = $node->appendChild( $longName );
		
		$ipAddr = $xml->createElement( 'addr', $n_addr );
		$ipAddr = $node->appendChild( $ipAddr );
	}
}

echo $xml->saveXML();

?>
